package com.weixing.mall.provider.support.mq;

import com.weixing.mall.core.utils.EnvUtil;
import com.weixing.mall.redis.RedisKeyConst;
import com.weixing.mall.redis.order.OrderPayEnd;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class OrderPayEndRedissonQueueProducer {


    private RedissonClient redissonClient;

    @Autowired
    public OrderPayEndRedissonQueueProducer(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }


    public void producer(OrderPayEnd payEnd) {
        //获取阻塞队列<名称和消费时获取名称一致>
        RBlockingDeque<OrderPayEnd> blockingDeque = redissonClient.getBlockingDeque(RedisKeyConst.QUEUE_ORDER_PAY_END);
        //加入延迟队列
        RDelayedQueue<OrderPayEnd> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        //将对象发送到redis延迟队列 ,20 单位秒 <这里单位可以选择的>
        delayedQueue.offer(payEnd, EnvUtil.isDev() ? 2 : 15, TimeUnit.MINUTES);
        // 使用后释放掉
        delayedQueue.destroy();
    }
}